package com.seek.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seek.eduservice.client.VodClient;
import com.seek.eduservice.entity.EduVideo;
import com.seek.eduservice.mapper.EduVideoMapper;
import com.seek.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author wml
 * @since 2022-01-16
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Autowired
    private VodClient vodClient;

    // 根据章节 id 查询小节的个数
    @Override
    public int getByChapterId(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        return baseMapper.selectCount(queryWrapper);
    }

    // 根据课程 id 删除小节
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.select("video_source_id");
        List<EduVideo> eduVideoList = baseMapper.selectList(queryWrapper);
        String videoSourceIds = "";
        String videoSourceId = null;
        for (EduVideo eduVideo : eduVideoList) {
            videoSourceId = eduVideo.getVideoSourceId();
            if (!(videoSourceId == null || videoSourceId.equals(""))) {
                videoSourceIds += videoSourceId + ",";
            }
        }
        if (!videoSourceIds.equals("")) {
            videoSourceIds = videoSourceIds.substring(0, videoSourceIds.length() - 1);
            vodClient.removeAlyVideoByVideoSourceId(videoSourceIds);
        }

        queryWrapper.clear();
        queryWrapper.eq("course_id", courseId);
        baseMapper.delete(queryWrapper);
    }
}
